package com.laiyizhan.common.mapper;

import com.laiyizhan.common.model.Kvdb;
import org.apache.ibatis.annotations.*;

public interface KvdbMapper {
    @Delete({
            "delete from t_kvdb",
            "where id = #{id,jdbcType=BIGINT}"
    })
    int deleteByPrimaryKey(Long id);

    @Insert({
            "insert into t_kvdb (`key`, `value`)",
            "values (#{key,jdbcType=VARCHAR}, #{value,jdbcType=VARCHAR})"
    })
    @SelectKey(statement = "SELECT LAST_INSERT_ID()", keyProperty = "id", before = false, resultType = Long.class)
    int insert(Kvdb record);

    @Select({
            "select",
            "id, key, value, create_time",
            "from t_kvdb",
            "where id = #{id,jdbcType=BIGINT}"
    })
    @ResultMap("BaseResultMap")
    Kvdb selectByPrimaryKey(Long id);

    int updateByPrimaryKeySelective(Kvdb record);

    @Update({
            "update t_kvdb",
            "set key = #{key,jdbcType=VARCHAR},",
            "value = #{value,jdbcType=VARCHAR},",
            "create_time = #{createTime,jdbcType=TIMESTAMP}",
            "where id = #{id,jdbcType=BIGINT}"
    })
    int updateByPrimaryKey(Kvdb record);

    @Select({
            "select",
            "id, `key`, `value`, create_time",
            "from t_kvdb",
            "where `key` = #{key,jdbcType=VARCHAR} order by create_time desc limit 1"
    })
    @ResultMap("BaseResultMap")
    Kvdb selectByKey(String key);

    @Select({
            "select count(1) from t_kvdb where `key` = #{key,jdbcType=VARCHAR} ",
            " and DATE_FORMAT(create_time,'%y-%M-%d') = DATE_FORMAT(now(),'%y-%M-%d')"
    })
    int count(String key);

    @Delete({
            "delete from t_kvdb",
            "where `key` = #{key,jdbcType=VARCHAR}"
    })
    int deleteByKey(String key);

    @Select({
            "select",
            "id, `key`, `value`, create_time",
            "from t_kvdb",
            "where `value` = #{value,jdbcType=VARCHAR} order by create_time desc limit 1"
    })
    Kvdb selectByValue(String value);
}